package com.sinochem.iot.rule.service;

import com.sinochem.iot.rule.mapper.TriggerRuleStoreMapper;
import com.sinochem.iot.rule.model.TriggerRuleStore;
import com.sinochem.iot.rule.model.TriggerRuleStoreExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class TriggerRuleStoreService {

    @Autowired
    private TriggerRuleStoreMapper triggerRuleStoreMapper;

    /**
     * 添加存储位置
     *
     * @param triggerRuleStore
     * @return
     */
    public int addTriggerRuleStore(TriggerRuleStore triggerRuleStore) {
        return triggerRuleStoreMapper.insertSelective(triggerRuleStore);
    }

    /**
     * 根据ruleId获取数据目的地
     *
     * @param ruleId
     * @return
     */
    public TriggerRuleStore getTriggerRuleStoreByRuleId(Integer ruleId) {
        TriggerRuleStoreExample example = new TriggerRuleStoreExample();
        example.createCriteria().andRuleIdEqualTo(ruleId);
        List<TriggerRuleStore> triggerRuleStoreList = triggerRuleStoreMapper.selectByExample(example);
        if (triggerRuleStoreList.size() > 0)
            return triggerRuleStoreList.get(0);
        return null;
    }

    /**
     * 修改存储位置
     *
     * @param triggerRuleStore
     * @return
     */
    public int updTriggerRuleStore(TriggerRuleStore triggerRuleStore) {

        triggerRuleStore.setUpdateTime(new Date());
        return triggerRuleStoreMapper.updateByPrimaryKeySelective(triggerRuleStore);
    }

    /**
     * 删除触发结果根据ruleId
     *
     * @param ruleId
     * @return
     */
    public int delTriggerRuleStore(Integer ruleId) {
        return triggerRuleStoreMapper.deleteByRuleId(ruleId);
    }
}
